package com.lemeng.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.lemeng.entity.PageResult;
import com.lemeng.entity.Result;
import com.lemeng.pojo.Album;
import com.lemeng.service.AlbumService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("album")
public class AlbumController {


    @Reference
    AlbumService albumService;


    @GetMapping("findAll")
    public List<Album> findAll(){
        return albumService.findAll();
    }

    @GetMapping("findPage")
    public PageResult<Album> findPage(Integer page, Integer size) {
        return albumService.findPage(page,size);
    }

    @PostMapping("findList")
    public List<Album> findList(@RequestBody Map<String, Object> serchMap) {
        return albumService.findList(serchMap);
    }
    @PostMapping("findPage")
    public PageResult<Album> findPage(Integer page, Integer size,@RequestBody Map<String, Object> serchMap) {
        return albumService.findPage(page,size,serchMap);
    }
    @GetMapping("findById")
    public Album findById(@RequestParam Integer id) {
        return albumService.findById(id);
    }

    @PostMapping("addImgItem")
    public Result addImgItem(@RequestBody Album album) {
        albumService.addImgItem(album);
        return new Result();
    }

    @PostMapping("add")
    public Result add(@RequestBody Album album) {
        albumService.add(album);
        return new Result();
    }
    @PostMapping("update")
    public Result update(@RequestBody Album album) {
        albumService.update(album);
        return new Result();
    }

    @GetMapping("delete")
    public Result delete(Integer id) {
        albumService.delete(id);
        return new Result();
    }
    @GetMapping("deleteAll")
    public Result delete(String ids) {

        String[] split = ids.split(",");
        for (String id : split) {
            albumService.delete(Integer.parseInt(id));
        }


        return new Result();
    }

    @PostMapping("removeImageItems")
    public Result removeImageItems(@RequestBody Map<String, String> serchMap){
        Long id = Long.parseLong(serchMap.get("id"));
        String uids = serchMap.get("uids");
        String[] split = uids.split(",");
        for (String uid : split) {
            albumService.removeImageItems(id,Long.parseLong(uid));
        }
        return new Result();
    }

    @PostMapping("moveAlbum")
    public Result moveAlbum(@RequestBody Map<String, String> serchMap){
        Long newId = Long.parseLong(serchMap.get("newId"));
        Long oldId = Long.parseLong(serchMap.get("oldId"));
        Long uid = Long.parseLong(serchMap.get("uid"));

        albumService.moveAlbum(newId,oldId,uid);

        return new Result();
    }


}
